/*
 * TestMap.java
 *
 * Copyright (C) 2012 Matt Falkoski
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.falko.android.pinhead;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.util.Log;

/**
 * @author matt
 * 
 */
public class TestMap {

	public TestMap() {
		tileSet_ = new TileSet();
	}

	protected boolean loadGrid() {

		VertexGrid grid = null;

		if (mapData_ == null) {
			Log.v("pinhead", "fuck   mapData_ == null");
			return false;
		}

		grid = new VertexGrid(mapWidth_, mapHeight_);
		float[][] uvWorkspace = new float[4][2];
		for (int tileY = 0; tileY < mapHeight_; tileY++) {
			for (int tileX = 0; tileX < mapWidth_; tileX++) {
				final float offsetX = tileX * tileSet_.tileWidth_;
				final float offsetY = tileY * tileSet_.tileHeight_;
				final long GID = mapData_[tileY][tileX];

				final float[] p0 = { offsetX, offsetY + tileSet_.tileHeight_,
						0.0f };
				final float[] p1 = { offsetX, offsetY, 0.0f };
				final float[] p2 = { offsetX + tileSet_.tileWidth_, offsetY,
						0.0f };
				final float[] p3 = { offsetX + tileSet_.tileWidth_,
						offsetY + tileSet_.tileHeight_, 0.0f };

				final float[][] positions = { p0, p1, p2, p3 };
				tileSet_.GIDToUV(GID, uvWorkspace);

				grid.set(tileX, tileY, positions, uvWorkspace);
			}
		}
		grid_ = grid;
		return true;
	}

	public void loadTileData(Context context, GL10 gl) {
		tileSet_.load(context, gl);
	}
static int sdx = 0;
	public void draw(GL10 gl) {

		if (grid_ == null) {
			loadGrid();
			// grid_.generateHardwareBuffers(gl);
		}

		gl.glBindTexture(GL10.GL_TEXTURE_2D, tileSet_.texture_.name);
		VertexGrid.BeginDrawingVertexGrid(gl, true);

		gl.glPushMatrix();
		gl.glLoadIdentity();
		
		gl.glTranslatef(sdx--, 0, 0.0f);

		grid_.draw(gl, true);

		gl.glPopMatrix();

		VertexGrid.EndDrawingVertexGrid(gl);
	}

	public void draw(GL10 gl, float scrollX, float scrollY, CameraSystem cam) {

		if (grid_ == null) {
			loadGrid();
			// grid_.generateHardwareBuffers(gl);
		}

		gl.glBindTexture(GL10.GL_TEXTURE_2D, tileSet_.texture_.name);	
		grid_.beginDrawingStrips(gl, true);

//		final float camX = cam.getScrollX();
//		final float camY = cam.getScrollY();
		final float worldPixelWidth = mapWidth_ * tileSet_.tileWidth_;
		final float worldPixelHeight = mapHeight_ * tileSet_.tileHeight_;
		
		int startX;
		if (scrollX >= 0) {
			startX = 0;
		} else {
			startX = (int) ((Math.abs(scrollX)  / worldPixelWidth) * mapWidth_);
		}
		
		int endX = (int) (((Math.abs(scrollX) + cam.getViewWidth()) / worldPixelWidth) * mapWidth_);
		endX = endX+1 > mapWidth_? mapWidth_ : endX+1;
		
		int startY;
		if (scrollY >= 0) {
			startY = 0;
		} else {
			startY = (int) ((Math.abs(scrollY) / worldPixelHeight) * mapHeight_);
		}
		
		int endY = (int) (((Math.abs(scrollY) + cam.getViewHeight()) / worldPixelHeight) * mapHeight_);
		endY = endY+1 > mapHeight_? mapHeight_ : endY+1;
		
		final int indexesPerTile = 6;
        final int indexesPerRow = mapWidth_ * indexesPerTile;
        final int startOffset = (startX * indexesPerTile);
        final int count = (endX - startX) * indexesPerTile;
        gl.glPushMatrix();
		gl.glLoadIdentity();
		
		gl.glTranslatef(
				scrollX,
				scrollY,
				0.0f);
		
		if (logOnce_) {
//			Log.v("pinhead", "startX: " + startX);
//			Log.v("pinhead", "endX: " + endX);
			Log.v("pinhead", "startY: " + startY);
			Log.v("pinhead", "endY: " + endY);
			Log.v("pinhead", "count: " + count);
			
			
		}
		
		
		
		
        for (int tileY = startY; tileY < endY && tileY < mapHeight_ && count > 0; tileY++) {
        	final int row = tileY * indexesPerRow;
        	grid_.drawStrip(gl, true, row  + startOffset, count);
        	
        	if (logOnce_) {
        		Log.v("pinhead", "Row: " + tileY);
        	}
        	
        }
//        logOnce_ = false;
        Log.v("pinhead", "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
		gl.glPopMatrix();

		VertexGrid.EndDrawingVertexGrid(gl);
	}

	boolean logOnce_ = true;
	protected VertexGrid grid_;
	protected TileSet tileSet_;
	protected long[][] mapData_;
	protected int mapWidth_; // in tiles
	protected int mapHeight_; // in tiles
}
